package cm.tl.pms.service;

import cm.tl.pms.entity.Attachment;
import cm.tl.pms.entity.Project;
import cm.tl.pms.entity.vo.req.ProjectAddReq;
import cm.tl.pms.entity.vo.req.ProjectQueryReq;
import cm.tl.pms.entity.vo.resp.ProjectResp;
import cm.tl.pms.mapper.ProjectMapper;
import cm.tl.pms.mapstruct.ProjectMapStruct;
import cm.tl.pms.service.base.AttachmentService;
import cm.tl.pms.util.StringUtils;
import cm.tl.pms.util.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Slf4j
@Service
public class ProjectService extends MPJBaseServiceImpl<ProjectMapper, Project> {

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectMapStruct projectMapStruct;
    @Autowired
    private AttachmentService attachmentService;

    @Transactional(rollbackFor = Exception.class)
    public boolean addProject(ProjectAddReq req) {
        Project project = projectMapStruct.toProject(req);
//        Long count = projectMapper.selectCount(Wrappers.<Project>lambdaQuery().eq(Project::getName, project.getName()));
//        if (count > 0) {
//            log.error("=addProject=>req:{} ,errorType:{}", req, PROJECT_NAME_EXIST);
//            throw new PmsException(PROJECT_NAME_EXIST);
//        }

        //fixme 项目编码生成规则？
        project.setCode(UuidUtils.uuid());
        super.save(project);
        Optional.ofNullable(req.getDeclareLetterId()).ifPresent(id->{
            Attachment attachment = new Attachment();
            attachment.setId(id);
            attachment.setRefId(project.getId());
            attachmentService.updateById(attachment);
        });
        Optional.ofNullable(req.getBudgetTableId()).ifPresent(id->{
            Attachment attachment = new Attachment();
            attachment.setId(id);
            attachment.setRefId(project.getId());
            attachmentService.updateById(attachment);
        });
        Optional.ofNullable(req.getDataCatalogId()).ifPresent(id->{
            Attachment attachment = new Attachment();
            attachment.setId(id);
            attachment.setRefId(project.getId());
            attachmentService.updateById(attachment);
        });
        Optional.ofNullable(req.getTechnicalProposalId()).ifPresent(id->{
            Attachment attachment = new Attachment();
            attachment.setId(id);
            attachment.setRefId(project.getId());
            attachmentService.updateById(attachment);
        });
        Optional.ofNullable(req.getOtherDataId()).ifPresent(id->{
            Attachment attachment = new Attachment();
            attachment.setId(id);
            attachment.setRefId(project.getId());
            attachmentService.updateById(attachment);
        });
        return true;
    }

    public boolean updateProject(ProjectAddReq req) {
        Project project = projectMapStruct.toProject(req);
        super.updateById(project);
        return true;
    }

    public ProjectResp getProject(Long id) {
        return projectMapStruct.toProjectResp(projectMapper.selectById(id));
    }

    public Page<ProjectResp> page(ProjectQueryReq req) {
        LambdaQueryWrapper<Project> wrapper = Wrappers.<Project>lambdaQuery()
                .eq(StringUtils.isNotEmpty(req.getCode()), Project::getCode, req.getCode())
                .eq(Project::getStatusCode, req.getStatusCode())
                .eq(Project::getTypeCode, req.getTypeCode())
                .eq(Project::getFundResource, req.getFundResource())
                .eq(Project::getDataShareType, req.getDataShareType());
        Page<Project> page = super.page(new Page<>(req.getCurrent(), req.getSize()), wrapper);
        return new Page<ProjectResp>(req.getCurrent(), req.getSize(), page.getTotal()).setRecords(projectMapStruct.toProjectResp(page.getRecords()));
    }
}
